app.controller('InterfaceCtrl', ['$scope','$http','$state','$stateParams',function($scope, $http,$state,$stateParams) {

			$http.get($scope.app.basePath + 'api/provider/find_all.json').then(
					function(response) {
						if (!response.data.success) {
							$scope.authError = response.data.errorMessage;
							
						} else {
							$scope.providers = response.data.data;
							
							if($stateParams.provider==0||$stateParams.provider==""){
								if($scope.providers.length>0){
									$state.go('app.interface.list',{provider:$scope.providers[0].id});
								}
							}
						}
					}, function(x) {
						$scope.authError = 'Server Error';
					});
			

		} ]);

app.controller('InterfaceListCtrl', [ '$scope', '$stateParams', '$http',function($scope, $stateParams, $http) {
			
	$scope.providerid = $stateParams.provider;
	
	
	
	if(!$scope.providerid){
		if($scope.providers&&$scope.providers.length>0){
			$scope.providerid = $scope.providers[0].id;
		}
	}
	if($scope.providerid){
		$http.get($scope.app.basePath + 'api/provider/get.json?id='+$scope.providerid).then(
				function(response) {
					if (!response.data.success) {
						$scope.authError = response.data.errorMessage;

					} else {
						$scope.displayProvider = response.data.data;
					}
				}, function(x) {
					$scope.authError = 'Server Error';
				});
	}
	
	
	if($scope.providerid){
		$http.get($scope.app.basePath + 'api/path/find_propertys.json?providerId='+$scope.providerid).then(
				function(response) {
					if (!response.data.success) {
						$scope.authError = response.data.errorMessage;

					} else {
						$scope.paths = response.data.data;
					}
				}, function(x) {
					$scope.authError = 'Server Error';
				});
	}
			
			
			$scope.delete = function(id){
				$http.post($scope.app.basePath + 'api/path/delete_id.json',{id:id}).then(
						function(response) {
							if (!response.data.success) {
								$scope.authError = response.data.errorMessage;

							} else {
								for(var i=0;i<$scope.paths.length;i++){
									if($scope.paths[i].id == id){
										$scope.paths.splice(i,1);
									}
								}
							}
						}, function(x) {
							$scope.authError = 'Server Error';
				});
			}
			


			
			
		} ]);

app.controller('InterfaceNewCtrl', [
		'$scope',
		'$http',
		'$state',
		function($scope, $http, $state) {
			$scope.init = function() {

				$scope.provider = {};
				$scope.providerCodeValues = [];
				$scope.myCroppedImage = '';
				var handleFileSelect = function(evt) {
					var file = evt.currentTarget.files[0];
					var reader = new FileReader();
					reader.onload = function(evt) {

						$scope.myImage = evt.target.result;
						$scope.$apply(function($scope) {
							$scope.myCroppedImage = evt.target.result;
						});
					};
					reader.readAsDataURL(file);
				};
				angular.element(document.querySelector('#fileInput')).on('change', handleFileSelect);
				
				$http.get($scope.app.basePath + 'api/provider/provider_code_values.json').then(
						function(response) {
							if (!response.data.success) {
								$scope.authError = response.data.errorMessage;

							} else {
								$scope.providerCodeValues = response.data.data;
							}
						}, function(x) {
							$scope.authError = 'Server Error';
						});
				
				
		
			}

			$scope.save = function() {
				$scope.provider.iconImager = $scope.myCroppedImage;
				
				
				$.ajax({
				    url:$scope.app.basePath + 'api/provider/save_provider.json',
				    type:'POST', //GET
				    async:true,    //或false,是否异步
				    data:$scope.provider,
				    timeout:5000,    //超时时间
				    dataType:'json',    //返回的数据格式：json/xml/html/script/jsonp/text
				    success:function(data,textStatus,jqXHR){
				    	if (!data.success) {
							$scope.$apply(function () {
							     　$scope.authError = response.data.errorMessage;
							      });
							
						
						} else {
							
							$state.go('app.interface.list');
						}
				    },
				    error:function(xhr,textStatus){
				        console.log('错误')
				        console.log(xhr)
				        console.log(textStatus)
				    }
				})
				
				
			}

		} ]);




app.controller('InterfaceKeyNewCtrl', ['$scope','$http','$state','$stateParams',function($scope, $http,$state,$stateParams) {
	$scope.init = function(){
		$scope.addState = false;
		
		$scope.providerid = $stateParams.provider;
		
		$scope.addData = {providerId:$scope.providerid};
		
		$http.get($scope.app.basePath + 'api/key/find_propertys.json?providerId='+$scope.providerid).then(
				function(response) {
					if (!response.data.success) {
						$scope.authError = response.data.errorMessage;

					} else {
						$scope.keys = response.data.data;
					}
				}, function(x) {
					$scope.authError = 'Server Error';
		});
	}
	
	$scope.save = function(){
		$http.post($scope.app.basePath + 'api/key/save.json',$scope.addData).then(
				function(response) {
					if (!response.data.success) {
						$scope.authError = response.data.errorMessage;

					} else {
						$scope.keys[$scope.keys.length] = response.data.data;
						$scope.addData = {providerId:$scope.providerid};
						$scope.addState = false;
					}
				}, function(x) {
					$scope.authError = 'Server Error';
		});
	}
	$scope.delete=function(id){
		if(id){
			$http.post($scope.app.basePath + 'api/key/delete_id.json',{id:id}).then(
					function(response) {
						if (!response.data.success) {
							$scope.authError = response.data.errorMessage;

						} else {
							for(var i=0;i<$scope.keys.length;i++){
								if($scope.keys[i].id == id){
									$scope.keys.splice(i,1);
								}
							}
						}
					}, function(x) {
						$scope.authError = 'Server Error';
			});
		}else{
			$scope.addData = {providerId:$scope.providerid};
			$scope.addState = false;
		}
		
	}
	
	
} ]);





app.controller('InterfacePathNewCtrl', ['$scope','$http','$state','$stateParams',function($scope, $http,$state,$stateParams) {
	$scope.init = function(){
		$scope.providerid = $stateParams.provider;
		$scope.path = {isValidate:false,providerId:$scope.providerid};
	}
	
	$scope.save = function(){ 
		$http.post($scope.app.basePath + 'api/path/save.json',$scope.path).then(
				function(response) {
					if (!response.data.success) {
						$scope.authError = response.data.errorMessage;
					} else {
						$state.go('app.interface.list',{provider:$scope.providerid})
					}
				}, function(x) {
					$scope.authError = 'Server Error';
		});
	}
	
	
	
} ]);
